# import operator
#
# from .getPublicData import *
# import time
# import json
#
#
# def getNowTime():
#     timeFormat = time.localtime()
#     yer = timeFormat.tm_year
#     mon = timeFormat.tm_mon
#     day = timeFormat.tm_mday
#     # hour = timeFormat.tm_hour
#     return yer, monthList[mon - 1], day
#
#
# def getUserCreateTime():
#     users = getAllUsers()
#     data = {}
#     for u in users:
#         if data.get(str(u.createTime), -1) == -1:
#             data[str(u.createTime)] = 1
#         else:
#             data[str(u.createTime)] += 1
#     result = []
#     for k, v in data.items():
#         result.append({
#             'name': k,
#             'value': v
#         })
#     return result
#
#
# def getUserTop6():
#     users = getAllUsers()
#
#     # def sort_fn(item):
#     #     return time.mktime(time.strptime(str(item.createTime), '%Y-%m-%d'))
#
#     # users = list(sorted(users, key=sort_fn, reverse=True))[:6]
#     return users
#
#
# def getAllTags():
#     jobs = JobInfo.objects.all()
#     users = User.objects.all()
#     educationsTop = '学历不限'
#     salaryTop = 0
#     salaryMonthTop = 0
#     practice = {}
#
#     for job in jobs:
#         if educations[job.educational] < educations[educationsTop]:
#             educationsTop = job.educational
#         if not job.practice == 1:
#             salary = json.loads(job.salary)[1]
#             if salaryTop < salary:
#                 salaryTop = salary
#         if int(job.salaryMonth) > salaryMonthTop:
#             salaryMonthTop = int(job.salaryMonth)
#         if practice.get(job.practice, -1) == -1:
#             practice[job.practice] = 1
#         else:
#             practice[job.practice] += 1
#     practiceMax = sorted(practice.items(), key=lambda x: x[1], reverse=True)
#     return len(jobs), len(users), educationsTop, salaryTop, salaryMonthTop, practiceMax[0][0]
#
#
# def getAllJobsPBar():
#     jobs = getAllJobs()
#     tempData = {}
#     for job in jobs:
#         if tempData.get(str(job.createTime), -1) == -1:
#             tempData[str(job.createTime)] = 1
#         else:
#             tempData[str(job.createTime)] += 1
#
#     def sort_fn(item):
#
#         item = list(item)
#         return item[0]
#
#     result = list(sorted(tempData.items(), key=sort_fn, reverse=False))
#     # print(result)
#     def map_fn(item):
#         item = list(item)
#         item.append(round(item[1] / len(jobs),3))
#         return item
#     result = list(map(map_fn, result))
#     # print(result)
#     return result
# def getTablaData():
#     jobs = getAllJobs()
#     for i in jobs:
#         i.workTag = '，'.join(json.loads(i.workTag))
#         if i.companyTags != "无":
#             i.companyTags = ''.join(json.loads(i.companyTags)[0])
#         if i.companyPeople == "[0,10000]":
#             i.companyPeople = "10000人以上"
#         else:
#             i.companyPeople = json.loads(i.companyPeople)
#             i.companyPeople = list(map(lambda x: str(x) + '人', i.companyPeople))
#             i.companyPeople = '-'.join(i.companyPeople)
#         i.salary = json.loads(i.salary)[1]
#     return jobs



import operator
from .getPublicData import *
import time
import json

def getNowTime():
    timeFormat = time.localtime()
    yer = timeFormat.tm_year
    mon = timeFormat.tm_mon
    day = timeFormat.tm_mday
    # hour = timeFormat.tm_hour
    return yer, monthList[mon - 1], day


def getUserCreateTime():
    users = getAllUsers()
    data = {}
    for u in users:
        if data.get(str(u.createTime), -1) == -1:
            data[str(u.createTime)] = 1
        else:
            data[str(u.createTime)] += 1
    result = []
    for k, v in data.items():
        result.append({
            'name': k,
            'value': v
        })
    return result


def getUserTop6():
    users = getAllUsers()

    # def sort_fn(item):
    #     return time.mktime(time.strptime(str(item.createTime), '%Y-%m-%d'))

    # users = list(sorted(users, key=sort_fn, reverse=True))[:6]
    return users


def getAllTags():
    jobs = JobInfo.objects.all()
    users = User.objects.all()
    educationsTop = '学历不限'
    salaryTop = 0
    salaryMonthTop = 0
    practice = {}

    for job in jobs:
        # 检查 job.educational 是否为 'nan'
        if job.educational != 'nan' and job.educational != 'None':
            if educations.get(job.educational, float('inf')) < educations.get(educationsTop, float('inf')):
                educationsTop = job.educational
        if not job.practice == 1:
            salary = json.loads(job.salary)[1]
            if salaryTop < salary:
                salaryTop = salary
        if int(job.salaryMonth) > salaryMonthTop:
            salaryMonthTop = int(job.salaryMonth)
        if practice.get(job.practice, -1) == -1:
            practice[job.practice] = 1
        else:
            practice[job.practice] += 1
    practiceMax = sorted(practice.items(), key=lambda x: x[1], reverse=True)
    return len(jobs), len(users), educationsTop, salaryTop, salaryMonthTop, practiceMax[0][0]


def getAllJobsPBar():
    jobs = getAllJobs()
    tempData = {}
    for job in jobs:
        if tempData.get(str(job.createTime), -1) == -1:
            tempData[str(job.createTime)] = 1
        else:
            tempData[str(job.createTime)] += 1

    def sort_fn(item):
        item = list(item)
        return item[0]

    result = list(sorted(tempData.items(), key=sort_fn, reverse=False))

    # print(result)

    def map_fn(item):
        item = list(item)
        item.append(round(item[1] / len(jobs), 3))
        return item

    result = list(map(map_fn, result))
    # print(result)
    return result


def getTablaData():
    jobs = getAllJobs()
    for i in jobs:
        i.workTag = '，'.join(json.loads(i.workTag))
        if i.companyTags != "无":
            i.companyTags = ''.join(json.loads(i.companyTags)[0])
        if i.companyPeople == "[0,10000]":
            i.companyPeople = "10000人以上"
        else:
            i.companyPeople = json.loads(i.companyPeople)
            i.companyPeople = list(map(lambda x: str(x) + '人', i.companyPeople))
            i.companyPeople = '-'.join(i.companyPeople)
        i.salary = json.loads(i.salary)[1]
    return jobs
